//
//  MyWishListViewController.m
//  何五路
//
//  Created by _ADY on 15/7/29.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "MyWishListViewController.h"

@interface MyWishListViewController ()

@end

@implementation MyWishListViewController
@synthesize aInt,mTableView,ShopArray,spArray,mTableView1;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = bgColor;
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"商品",@"店铺", nil]];
    segmentedControl.frame = CGRectMake(0, 0,  120, 30);
    segmentedControl.selectedSegmentIndex = aInt;
    segmentedControl.tintColor = [UIColor grayColor];
    [segmentedControl addTarget:self action:@selector(segmentedSelect:) forControlEvents:UIControlEventValueChanged];
    [self.navigationItem setTitleView:segmentedControl];
    
    wishScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height-64)];
    wishScrollView.backgroundColor = [UIColor clearColor];
//    [wishScrollView setDelegate:self];
    [wishScrollView setPagingEnabled:YES];
    wishScrollView.scrollEnabled = NO;
    [wishScrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:wishScrollView];
    
    [wishScrollView setContentSize:CGSizeMake(self.view.bounds.size.width*2,self.view.bounds.size.height-64)];
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64)];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.backgroundColor = [UIColor clearColor];
    [wishScrollView addSubview:mTableView];
    
    __weak __typeof(self) weakSelf = self;
    
    mTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    mTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    
    mTableView1 = [[UITableView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStyleGrouped];
    mTableView1.delegate = self;
    mTableView1.dataSource = self;
    mTableView1.backgroundColor = [UIColor colorWithWhite:.95 alpha:1];
    [wishScrollView addSubview:mTableView1];
    
    __weak __typeof(self) weakSelf1 = self;
    
    mTableView1.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf1 loadNewData];
    }];
    mTableView1.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    if (aInt == 0) {
        [mTableView.header beginRefreshing];
    }
    else
    {
        [mTableView1.header beginRefreshing];
    }
    [wishScrollView setContentOffset:CGPointMake(self.view.bounds.size.width*aInt, 64) animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}
- (void)segmentedSelect:(UISegmentedControl *)sender
{
    //    segmentedControl.selectedSegmentIndex = ((UISegmentedControl *)sender).selectedSegmentIndex;
    aInt = segmentedControl.selectedSegmentIndex;
   [wishScrollView setContentOffset:CGPointMake(self.view.bounds.size.width*aInt, 0) animated:YES];
    if (aInt == 0) {
        if (spArray.count == 0) {
            [mTableView.header beginRefreshing];
        }
        else
            [mTableView reloadData];
    }
    else
    {
        if (ShopArray.count == 0) {
            [mTableView1.header beginRefreshing];
        }
        else
            [mTableView1 reloadData];
    }

}
- (void)loadNewData
{
    if (aInt == 0) {
        pageForNumber = 1;
    }
    else
        pageForNumber1 = 1;
    [self upDateTableView];
}
- (void)loadMoreData
{
    if (aInt == 0) {
        pageForNumber ++;
    }
    else
        pageForNumber1 ++;
    [self upDateTableView];
}

-(void)upDateTableView
{
    if (aInt == 0)
    {
        if ((pageForNumber-1)*itemLabel <= [spArray count]||pageForNumber ==1)
        {
            NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
            [postDic setObject:[NSNumber numberWithInt:pageForNumber] forKey:@"command.PageNumber"];
            [postDic setObject:[NSNumber numberWithInt:itemLabel] forKey:@"command.PageSize"];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                NSDictionary *ret = [SendService Wishlist:postDic];
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    NSLog(@"ret %@",ret);
                    if (ret !=nil)
                    {
                        if (pageForNumber == 1)
                        {
                            if (spArray != nil)
                            {
                                [spArray removeAllObjects];
                                spArray = nil;
                            }
                            spArray = [[NSMutableArray alloc] init];
                            
                        }
                        for (NSDictionary *pic in [[ret objectForKey:@"Entity"] objectForKey:@"Items"])
                        {
                            [spArray addObject:pic];
                        }
                        
                        
                    }
                    if (pageForNumber ==1)
                        [mTableView.header endRefreshing];
                    else
                        [mTableView.footer endRefreshing];
                    [mTableView reloadData];
                    
                });
            });
            
        }
        else
        {
            [mTableView.footer noticeNoMoreData];
            [mTableView reloadData];
        }
    }
    else
    {
        if ((pageForNumber1-1)*itemLabel <= [ShopArray count]||pageForNumber1 ==1)
        {
            NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
            [postDic setObject:[NSNumber numberWithInt:pageForNumber1] forKey:@"command.PageNumber"];
            [postDic setObject:[NSNumber numberWithInt:itemLabel] forKey:@"command.PageSize"];
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                NSDictionary *ret = [SendService FavoriteShopList:postDic];
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    NSLog(@"ret %@",ret);
                    if (ret !=nil)
                    {
                        if (pageForNumber1 == 1)
                        {
                            if (ShopArray != nil)
                            {
                                [ShopArray removeAllObjects];
                                ShopArray = nil;
                            }
                            ShopArray = [[NSMutableArray alloc] init];
                            
                        }
                        for (NSDictionary *pic in [[ret objectForKey:@"Entity"] objectForKey:@"Items"])
                        {
                            [ShopArray addObject:pic];
                        }
                        
                        
                    }
                    if (pageForNumber1 ==1)
                        [mTableView1.header endRefreshing];
                    else
                        [mTableView1.footer endRefreshing];
                    [mTableView1 reloadData];
                    
                });
            });
            
        }
        else
        {
            [mTableView1.footer noticeNoMoreData];
            [mTableView1 reloadData];
        }
    }

}

#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (aInt == 0)
        return 1;
    return ShopArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
     if (aInt == 0)
         return spArray.count;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (aInt == 0)
        return self.view.bounds.size.height/4;
    else
    {
        if (indexPath.row == 0) {
            return  50;
        }
        else
            return 70;
    }
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyWishList";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
    }
    if (aInt == 0)
        cell.backgroundColor = [UIColor colorWithWhite:.95 alpha:1];
    else
        cell.backgroundColor = [UIColor whiteColor];
    
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    if (aInt == 0)
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else
        return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSDictionary *dicS = [ShopArray objectAtIndex:indexPath.section];
    NSLog(@"dicS %@",dicS);
    if (indexPath.row == 0)
    {
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10,5, 100, 40)];
        [imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dicS objectForKey:@"VendorImgUrl"]]]];
        [cell.contentView addSubview:imageView1];
        
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(110, 5, self.view.bounds.size.width-120, 30) setTitle:[NSString stringWithFormat:@"%@",[dicS objectForKey:@"VendorName"]] setFont:labelSize+2 setColor:[UIColor blackColor]]];
#pragma mark 暂无数据
        [cell.contentView addSubview:[self setFrame:CGRectMake(110, 35, self.view.bounds.size.width-120, 10) setTitle:[NSString stringWithFormat:@"%@人关注",[dicS objectForKey:@"VendorId"]] setFont:labelSize-2 setColor:[UIColor grayColor]]];
        
    }
    else
    {
        UIScrollView *bScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, 70)];
        bScrollView.backgroundColor = [UIColor whiteColor];
        [bScrollView setShowsHorizontalScrollIndicator:NO];
        [cell.contentView addSubview:bScrollView];
        
        int type = 3;
        [bScrollView setContentSize:CGSizeMake(10+140*type,70)];
        
        for (int i = 0; i < type; i ++) {
            UIButton *gzButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [gzButton setFrame:CGRectMake(10+140*i, 5, 130, 60)];
            [gzButton setImage:[UIImage imageNamed:@"Shop_image1"] forState:UIControlStateNormal];
            gzButton.tag = 110+i;
            [gzButton addTarget:self action:@selector(hyjAction:) forControlEvents:UIControlEventTouchUpInside];
            [bScrollView addSubview:gzButton];
        }
    }
   return cell;
}

//优惠券
-(void)hyjAction:(id)sender
{
    //    int topType = ((UIButton*)sender).tag - 110;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [spArray objectAtIndex:indexPath.row];
    
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(30,10, 100, self.view.bounds.size.height/4-20)];
    [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[dic objectForKey:@"Picture" ] objectForKey:@"ImageUrl"]]]];
    bgImageView.contentMode = UIViewContentModeScaleToFill;
    bgImageView.clipsToBounds  = YES;
    bgImageView.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:bgImageView];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(140, 10, self.view.bounds.size.width-150, 65) setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"ProductName"]] setFont:labelSize setColor:[UIColor blackColor]]];
    
    [cell.contentView addSubview:[self setFrame:CGRectMake(140, self.view.bounds.size.height/4-50, self.view.bounds.size.width-150, 20) setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"UnitPrice"]] setFont:labelSize-2 setColor:[UIColor colorWithRed:241/255.0 green:83/255.0 blue:83/255.0 alpha:1]]];
    
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font setColor:(UIColor*)myColor
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = myColor;
    return titleLabel;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (aInt == 1) {
        
        if (indexPath.row == 0)
        {
           NSDictionary *dicS = [ShopArray objectAtIndex:indexPath.section];
            ShopsViewController *details = [[ShopsViewController alloc] init];
            details.shopsId = [NSString stringWithFormat:@"%@",[dicS objectForKey:@"VendorId"]];
            details.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:details animated:YES];
        }
        return;
    }
    GoodsDetailsViewController *details = [[GoodsDetailsViewController alloc] init];
    details.productId = [NSString stringWithFormat:@"%@",[[spArray objectAtIndex:indexPath.row] objectForKey:@"ProductId"]];
    details.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:details animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
